<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <!--<link rel="shortcut icon" href="img/favicon.ico">-->
    <!--<link rel="apple-touch-icon" href="img/favicon.ico">-->
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="http://cdn.bootcss.com/bootstrap-validator/0.5.3/css/bootstrapValidator.min.css" rel="stylesheet" />

</head>

<body>
<form class="form form-horizontal" autocomplete="off" action="login.html" method="post">
    <div class="form-group">
        <label>手机号</label>
        <input type="text" class="form-control" name="tel" />
    </div>
    <div class="form-group">
        <label>验证码</label>
        <input type="text" class="form-control" name="yan" />
        <button type="button" class="btn btn-primary radius size-L">获取验证码</button>
    </div>
    <div class="form-group">
        <button type="button" id="submit" name="submit" class="btn btn-primary">提交</button>
    </div>
</form>


<script src="../lib/jquery/1.9.1/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js"></script>
<script>
    $('form').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            tel: {
                validators: {
                    notEmpty: {
                        message: '手机号不能为空'
                    },
                    regexp: {
                        regexp: /^1\d{10}$/,
                        message: '手机号格式错误'
                    }
                }
            },
            yan: {
                validators: {
                    notEmpty: {
                        message: '验证码不能为空'
                    },
                    regexp: {
                    }
                }
            }
        }
    });

    var bootstrapValidator = $('form').data('bootstrapValidator');

    // 提交时验证
    $('#submit').on('click', function () {
        bootstrapValidator.validate();
        if (bootstrapValidator.isValid()) {
            //表单提交的方法、比如ajax提交
            alert('success');
        }
    })

    // 验证手机号
    function checkMobile(str) {
        let re = /^1\d{10}$/
        if (re.test(str)) {
            return true;
        } else {
            return false;
        }
    }

</script>

</body>

</html>